<?php

namespace app\index\controller;

use coser\Controller;
use app\index\model\TaskModel;

class Task extends Controller
{
    public function index()
    {
        $taskModel = new TaskModel();

        $menu_list = $this->taskMenuList();
		$task_list = $this->taskTaskList();
        $board_list = $taskModel->getMeunBoard();
        var_dump($board_list);exit;
		$this->assign('hi',':)');
		$this->assign('menu_list',$menu_list);
		$this->assign('task_list',$task_list);
		$this->display('task/index');
    }

    //添加任务
	public function addTask()
	{
		$data = $_POST;
		
		//获取主任务
		$taskModel = new TaskModel();
        //检查主任务是否完成
        $mu_where = [
            't1.id' => $data['id'],
            't1.uid' => 1,
            't1.is_del' => 1,
        ];
        $on = 't1.id = t2.pid and t2.is_del = 2 and t2.status = 1';
		$taskMenu = $taskModel->getMenuInfo($mu_where,$on);
		if(empty($taskMenu)) return ajaxReturn(['code'=>400,'msg'=>'主任务不存在']);
		
		$task_data = [
			'uid' => 1,
			'title' => $taskMenu['title'],
			'pid' => $taskMenu['id'],
			'avg_time' => $taskMenu['avg_time'],
			'remai_time' => $taskMenu['avg_time'] * 60,
			'create_time' => time(),
		];
		
		$taskModel->addTask($task_data);

        return ajaxReturn(['code'=>200,'msg'=>'添加成功']);
	}

    //修改任务
    public function editTask()
    {
        $data = $_POST;

        $taskModel = new TaskModel();
        $task = $taskModel->getTaskInfo(['id'=>$data['id']]);
        if(empty($task)) return ajaxReturn(['code'=>400,'msg'=>'任务不存在']);

        $where = ['id'=>$data['id']];

        if(isset($data['remai_time'])){
            if($data['remai_time'] <= 0){
                $data['status'] = 3;
                $data['end_time'] = time();

                //检查主任务是否完成
                $mu_where = [
                    't1.id' => $task['pid'],
                    't1.uid' => 1,
                    't1.is_del' => 1,
                ];
                $on = 't1.id = t2.pid and t2.is_del = 2 and t2.status = 1';
                $taskMenu = $taskModel->getMenuInfo($mu_where,$on);

                //判断任务量是否完成
                if($taskMenu['chapter'] >= ($taskMenu['ok_chapter'] + 1)){
                    $up_menu['status'] = 3;

                    $taskModel->editMenu(['id'=>$task['pid']],$up_menu);
                }
            }
        }
        if(!empty($task['start_time'])) unset($data['start_time']);

        $taskModel->editTask($where,$data);

        return ajaxReturn(['code'=>200,'msg'=>'修改成功']);
    }


	//添加主任务
	public function addTaskMenu()
	{
		$data = $_POST;

		$data['uid'] = 1;
        $data['start_time'] = time();

		$taskModel = new TaskModel();
		$taskModel->addMenu($data);
		//检查类型 无则添加
        $task_type_data = [
            'title' => $data['type'],
            'uid' => 1,
        ];
        $is_psne = $taskModel->checkType($task_type_data);
        if(!$is_psne){
            $task_type_data['create_time'] = time();
            $taskModel->addType($task_type_data);
        }

        return ajaxReturn(['code'=>200,'msg'=>'添加成功']);
	}
	
	//获取任务列表
	public function taskTaskList()
	{
		$where = [
			'uid' => 1,
            'is_del' => 1,
            'status' => ['<>',3],
		];

        $taskModel = new TaskModel();
        $list =$taskModel->getTasklist($where);

        $status = ['1'=>'未完成','2'=>'执行中','3'=>'完成'];
        $status_class = ['1'=>'role user','2'=>'role user','3'=>'role member','4'=>'role admin'];
        foreach($list as $key => $val){
            $list[$key]['avg_time'] = $val['avg_time'].':00';
            $list[$key]['status'] = $status[$val['status']];
            $list[$key]['status_class'] = $val['create_time'] + '259200' > time() ?  $status_class[$val['status']] : 'role admin';
            $list[$key]['remai_times'] = intval($val['remai_time'] / 60) .':'. paddZero($val['remai_time'] % 60);
        }

        return $list;
    }

    //获取主任务列表
    public function taskMenuList()
    {
        $field = 't1.*,count(t2.id) as ok_chapter';
        $where = [
            't1.uid' => 1,
            't1.is_del' => 1,
            't1.status' => 1,
        ];
        $on = 't1.id = t2.pid and t2.is_del = 1 and t2.status = 3';

        $taskModel = new TaskModel();
        $list =$taskModel->getMenulist($field,$where,$on);
        foreach($list as $key => $val){
            $list[$key]['title'] = strlen($val['title']) > 12 ? mb_substr($val['title'],0,12).'...':$val['title'];
        }

        return $list;
    }

    //修改主任务
    public function editTaskMenu()
    {
        $data = $_POST;

        $where = ['id'=>$data['id']];

        $taskModel = new TaskModel();
        $taskModel->editTaskMenu($where,$data);

        return ajaxReturn(['code'=>200,'msg'=>'修改成功']);
    }

}